package com.yeebo.utils;


import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * 自定义时间处理工具类
 */
public class DateUtils {
    public static final String YYYY_MM_DD = "yyyy-MM-dd";
    public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmssSSS";
    public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

    public static List<Date> getDatesBetweenTwoDate(Date beginDate, Date endDate) {
        List<Date> lDate = new LinkedList<>();
        lDate.add(beginDate);//把开始时间加入
        Calendar cal = Calendar.getInstance();
        cal.setTime(beginDate);
        while (true) {
            //根据日历的规则，为给定的日历字段添加或减去指定的时间量
            cal.add(Calendar.DAY_OF_MONTH, 1);
            // 测试此日期是否在指定日期之后
            if (endDate.after(cal.getTime())) {
                lDate.add(cal.getTime());
            } else {
                break;
            }
        }
        lDate.add(endDate);//把结束时间加入
        return lDate;
    }

    public static String getYMDHMSS() {
        String orderNo = new SimpleDateFormat(YYYYMMDDHHMMSS).format(new Date());
        return orderNo;
    }

    public static Date stringToDate(String timeStr, String format) {
        Date targetDate = null;
        if (StringUtils.isNotBlank(timeStr)) {
            try {
                SimpleDateFormat sdf = new SimpleDateFormat(format);
                targetDate = sdf.parse(timeStr);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return targetDate;
    }

    public static void main(String[] args) {
        System.out.println(getYMDHMSS());
    }

}
